package com.qd.panda.plate;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.cdqidi.valid.group.UpdateGroup;
import com.qd.common.panda.constant.PandaConstant;
import com.qd.common.panda.domain.entity.plate.UserPlateDTO;
import com.qd.common.panda.domain.entity.plate.UserPlateRelationDTO;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.common.panda.domain.entity.plate.UserPlateApprovalDTO;
import com.qd.panda.service.plate.PlateHolderService;
import com.qd.panda.service.plate.UserPlateApprovalService;
import com.qd.panda.service.plate.UserPlateRelationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * APP_车牌管理
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/app/plate")
@Slf4j
public class AppPlateController {
    private final PlateHolderService plateHolderService;
    private final UserPlateRelationService userPlateRelationService;
    private final UserPlateApprovalService userPlateApprovalService;

    /**
     * 添加车牌
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PostMapping(value = "/save")
    public UserPlateRelationDTO save(@Validated({DefaultGroup.class}) @RequestBody UserPlateDTO dto) {
        return plateHolderService.save(dto);
    }

    /**
     * 修改车牌
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PutMapping(value = "/update")
    public UserPlateRelationDTO update(@Validated({UpdateGroup.class, DefaultGroup.class}) @RequestBody UserPlateDTO dto) {
        return plateHolderService.update(dto);
    }

    /**
     * 删除车牌
     *
     * @param delDTO 主键，多个用分号隔开
     * @return 页面对象
     */
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            userPlateRelationService.removeById(ids.get(0));
        } else {
            userPlateRelationService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 获取车牌
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public UserPlateRelationDTO getById(@Validated @NotBlank(message = DelDTO.ID_NULL_MSG) String id) {
        return userPlateRelationService.getByIdDto(id);
    }

    /**
     * 车牌列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<UserPlateRelationDTO> page(UserPlateRelationDTO dto) {
        Optional.ofNullable(SecurityUtils.getLoginUser(false)).ifPresent(loginUser -> dto.setUserId(loginUser.getUserId()));
        return userPlateRelationService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 提交审核
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PostMapping(value = "/plateApproval")
    public ApiResultWrapper<Object> plateApproval(@Validated({DefaultGroup.class}) @RequestBody UserPlateApprovalDTO dto) {
        dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        dto.setUserMobile(SecurityUtils.getLoginUser().getUser().getAppUserExtend().getMobile());
        dto.setAddTime(LocalDateTime.now());
        dto.setApprovalStatus(PandaConstant.APPROVAL_STATUS_0);
        userPlateApprovalService.plateApproval(dto);
        dto.freeData();
        return ApiResult.success();
    }

    /**
     * 审批列表
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @GetMapping(value = "/plateApprovalPage")
    public IPage<UserPlateApprovalDTO> plateApprovalPage(UserPlateApprovalDTO dto) {
        dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        return userPlateApprovalService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 审批详情
     *
     * @param approvalId 待审核ID
     * @return 页面对象
     */
    @GetMapping(value = "/plateApprovalDetail")
    public UserPlateApprovalDTO plateApprovalDetail(@Validated @NotBlank(message = "待审核ID不能为空") String approvalId) {
        return userPlateApprovalService.getByIdDto(approvalId);
    }

    /**
     * 删除审批记录
     *
     * @param delDTO 待审核ID
     * @return 页面对象
     */
    @DeleteMapping(value = "/deletePlateApproval")
    public ApiResultWrapper<Object> deletePlateApproval(@Validated @RequestBody DelDTO<String> delDTO) {
        final List<String> ids = delDTO.getIds();
        userPlateApprovalService.deletePlateApproval(ids);
        return ApiResult.success();
    }
}